package com.graduation.fms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.graduation.fms.dao.Fk;
import com.graduation.fms.mapper.FkMapper;
import com.graduation.fms.utils.DateUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MyBatisPlusAutoGenerator
 * @since 2021-05-22
 */
@RestController
@RequestMapping("/fms/fk")
@CrossOrigin(origins = "*",allowCredentials ="true")
public class FkController {

    @Resource
    private FkMapper fkMapper;

    @RequestMapping("/getFk")
    public List<Map<String, Object>> getFk(@RequestBody Map<String, String> params) throws Exception {
        String key = params.get("key");
        if (key.isEmpty()) {
            List<Map<String, Object>> fk = fkMapper.getFk();
            for (Map<String, Object> map : fk) {
                map.put("date", DateUtils.D2NYR(map.get("createTime")));
            }
            return fk;
        }
        List<Map<String, Object>> fk = fkMapper.getFkByKey(key);
        for (Map<String, Object> map : fk) {
            map.put("date", DateUtils.D2NYR(map.get("createTime")));
        }
        return fk;
    }

    @RequestMapping("/getFkByUserId")
    public List<Fk> getFkByUserId(@RequestBody Map<String, String> params) {
        String userId = params.get("userId");
        QueryWrapper<Fk> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return fkMapper.selectList(wrapper);
    }

    @RequestMapping("/addFk")
    public Map<String, Object> addFk(@RequestBody Map<String, String> params) {
        String userId = params.get("userId");
        String msg = params.get("msg");
        Map<String, Object> result = new HashMap<>();
        if (userId.isEmpty() || msg.isEmpty()) {
            result.put("msg", "参数不能为空");
            return result;
        }
        Fk fk = new Fk();
        fk.setUserId(Integer.valueOf(userId));
        fk.setMsg(msg);
        fkMapper.insert(fk);
        result.put("success", true);
        return result;
    }
}

